﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;

namespace Mimoza.NetworkPlugin
{
    public partial class NetworkSettingsPage : Mimoza.GUI.WizardPage
    {
        public NetworkSettingsPage()
        {
            InitializeComponent();
        }

        NetworkPluginData m_data;

        override public void Init()
        {
            m_data = this.Wizard.UserData as NetworkPluginData;

            checkBoxUseDefaultGateway.Checked = m_data.IsGatewayDefined;
            checkBoxUseDefaultGateway_CheckedChanged(null, null);

            checkBoxUseDhcp.Checked = m_data.IsDHCPActive;
            checkBoxUseDhcp_CheckedChanged(null, null);

            radioButtonAutoDNS.Checked = m_data.IsAutoDns;
            radioButtonUseDns.Checked = m_data.IsDnsDefined;
            radioButtonUseDns_CheckedChanged(null, null);

            this.ipAddressControlDefGateway.Text = m_data.GatewayAddress;
            this.ipAddressControlPrefDns.Text = m_data.PrefferedDns;
            this.ipAddressControlAltDns.Text = m_data.AlternateDns;


            this.checkBoxUseDhcp.CheckedChanged += new System.EventHandler(this.checkBoxUseDhcp_CheckedChanged);
            this.checkBoxUseDefaultGateway.CheckedChanged += new System.EventHandler(this.checkBoxUseDefaultGateway_CheckedChanged);
            this.radioButtonUseDns.CheckedChanged += new System.EventHandler(this.radioButtonUseDns_CheckedChanged);
            this.ipAddressControlAltDns.TextChanged += new System.EventHandler(this.ipAddressControlPrefDns_TextChanged);
            this.ipAddressControlPrefDns.TextChanged += new System.EventHandler(this.ipAddressControlPrefDns_TextChanged);
            this.ipAddressControlDefGateway.TextChanged += new System.EventHandler(this.ipAddressControlPrefDns_TextChanged);
        }

        override public bool Try2LeavePage(WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Back)
            {
                return true;
            }

            m_data.IsDHCPActive = checkBoxUseDhcp.Checked;
            m_data.IsAutoDns = radioButtonAutoDNS.Checked;
            m_data.IsDnsDefined = radioButtonUseDns.Checked;

            m_data.GatewayAddress = this.ipAddressControlDefGateway.GetPureIPAddress();
            m_data.PrefferedDns = this.ipAddressControlPrefDns.GetPureIPAddress();
            m_data.AlternateDns = this.ipAddressControlAltDns.GetPureIPAddress();

            
            return true;
        }

        private void checkBoxUseDefaultGateway_CheckedChanged(object sender, EventArgs e)
        {
            labelGateway.Enabled = checkBoxUseDefaultGateway.Checked;
            ipAddressControlDefGateway.Enabled = checkBoxUseDefaultGateway.Checked;

            if (null != sender)
            {
                this.RaiseOnPageModified();
            }
        }

        private void radioButtonUseDns_CheckedChanged(object sender, EventArgs e)
        {
            labelPreferredDns.Enabled = radioButtonUseDns.Checked;
            labelAltDns.Enabled = radioButtonUseDns.Checked;
            ipAddressControlPrefDns.Enabled = radioButtonUseDns.Checked;
            ipAddressControlAltDns.Enabled = radioButtonUseDns.Checked;

            if (null != sender)
            {
                this.RaiseOnPageModified();
            }
        }

        private void checkBoxUseDhcp_CheckedChanged(object sender, EventArgs e)
        {
            checkBoxUseDefaultGateway.Checked = false;
            checkBoxUseDefaultGateway.Enabled = !checkBoxUseDhcp.Checked;

            checkBoxUseDefaultGateway_CheckedChanged(null, null);

            if (null != sender)
            {
                this.RaiseOnPageModified();
            }
        }

        private void ipAddressControlPrefDns_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void ipAddressControlPrefDns_TextChanged_1(object sender, EventArgs e)
        {

        }
    }
}
